import json

from flask import Blueprint,Response
from flask import render_template
from flask import request
from flask import jsonify


# /core/user/add_role_to_users/
from models.entity.button import Button
from models.entity.role_with_button import RoleWithButton
from models.entity.role_with_module import RoleWithModule
from utils.result import result

mod = Blueprint("role", __name__, url_prefix="/core/role", template_folder="templates", static_folder="static")

@mod.route("/get_modules_by_role/", methods=["GET", "POST"])
def get_modules_by_role():
    """
    {'role_id':1}
    :return: [module_id, ...]
    """
    try:
        data = json.loads(request.data.decode("utf-8"))
    except:
        error_msg = {
            "success": 0,
            "error_code": -1,
            "error_msg": "json format error"
        }
        return jsonify(error_msg)
    role_id = data.get('role_id', '')
    print('role_id', role_id)
    modules = RoleWithModule.get_role_with_modules_by_role_id(role_id)
    rdic = {}
    rdic["data"] = []
    for item in modules:
        print('item', item)
        rdic["data"].append(item.id)
    print(rdic["data"])
    # return modules
    return result(data=rdic["data"])


@mod.route("/get_buttons_by_role/", methods=["GET", "POST"])
def get_buttons_by_role():
    """
    {'role_id':1}
    :return: [button_id, ...]
    """
    try:
        data = json.loads(request.data.decode("utf-8"))
    except:
        error_msg = {
            "success": 0,
            "error_code": -1,
            "error_msg": "json format error"
        }
        return jsonify(error_msg)
    role_id = data.get('role_id', '')
    print('role_id', role_id)
    buttons = Button.get_role_with_buttons_by_role_id(role_id)
    rdic = {}
    rdic["data"] = []
    for item in buttons:
        print('item', item)
        rdic["data"].append(item.id)
    print(rdic["data"])
    return result(data=rdic["data"])


@mod.route("/update_modules_by_role/", methods=["GET", "POST"])
def update_modules_by_role():
    """
    {'role_id':4, 'module_ids':[module_id...]}
    :return:
    """
    try:
        data = json.loads(request.data.decode("utf-8"))
    except:
        error_msg = {
            "success": 0,
            "error_code": -1,
            "error_msg": "json format error"
        }
        return jsonify(error_msg)
    role_id = data.get('role_id', '')
    module_ids = data.get('module_ids', '')
    print('role_id', role_id)
    print('button_ids', module_ids)
    connectors = RoleWithModule.query.filter_by(role_id=role_id).all()
    # connectors = RoleWithButton.query.filter_by(role_id=role_id).all()
    print(len(connectors), type(connectors))
    # print('connectors', connectors)
    print(111111)
    for item in connectors:
        # print(222,int(item))
        if item.delete() != 0:
            return jsonify('删除错误')
    print('删除成功')
    print(3333)
    RoleWithModule.add_modules_to_role_id(role_id,module_ids)
    return result(data=[])


@mod.route("/update_buttons_by_role/", methods=["GET", "POST"])
def update_buttons_by_role():
    """
    {'role_id':4, 'button_ids':[button_id...]}
    :return: [button_id, ...]
    """
    try:
        data = json.loads(request.data.decode("utf-8"))
    except:
        error_msg = {
            "success": 0,
            "error_code": -1,
            "error_msg": "json format error"
        }
        return jsonify(error_msg)
    role_id = data.get('role_id', '')
    button_ids = data.get('button_ids', '')
    print('role_id', role_id)
    print('button_ids', button_ids)
    connectors = RoleWithButton.query.filter_by(role_id=role_id).all()
    print(len(connectors))
    print('connectors', connectors)
    for item in connectors:
        if item.delete() != 0:
            return jsonify('删除错误')
    print('删除成功')
    RoleWithButton.add_buttons_to_role_id(role_id,button_ids)
    return result(data=[])


